package com.ruicar.afs.cloud.channel.grade.task;

import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.ruicar.afs.cloud.channel.grade.ao.GradeModelAO;
import com.ruicar.afs.cloud.channel.grade.constant.Constants;
import com.ruicar.afs.cloud.channel.grade.dto.GradeBigDataQuotaDTO;
import com.ruicar.afs.cloud.channel.grade.entity.*;
import com.ruicar.afs.cloud.channel.grade.service.*;
import com.ruicar.afs.cloud.channel.grade.utils.SFTPChannel;
import com.ruicar.afs.cloud.channel.grade.vo.GradeDataETVO;
import com.ruicar.afs.cloud.channel.grade.vo.GradeResultReturnVO;
import com.ruicar.afs.cloud.common.job.core.biz.model.ReturnT;
import com.ruicar.afs.cloud.common.job.core.handler.annotation.AfsJob;
import com.ruicar.afs.cloud.common.job.core.handler.annotation.AfsJobHandler;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.apache.commons.lang.time.DateFormatUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.transaction.interceptor.TransactionAspectSupport;

import java.io.BufferedReader;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.math.BigDecimal;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.*;
import java.util.logging.Logger;

/**
 * @Description: 自动解析大数据返回数据, 评分
 * @param:
 * @return:
 * @auther: jiangxiongfei
 * @date: 2020-08-14 11:01
 */
@AfsJob
@AllArgsConstructor
@Slf4j(topic = "")
@Component
public class GradeJob {

    @Value("${com.greatwall.sftp.ip}")
    private String ip = "";
    @Value("${com.greatwall.sftp.user}")
    private String user = "";
    @Value("${com.greatwall.sftp.pwd}")
    private String pwd = "";
    @Value("${com.greatwall.sftp.sftpReadDir}")
    private String sftpReadDir = "";
    @Value("${com.greatwall.sftp.downLoadFileSavePath}")
    private String downLoadFileSavePath = ""; //本地保存路径

    @Autowired
    private GradeModelResultService gradeModelResultService;
    @Autowired
    private GradeBigDataQuotaService gradeBigDataQuotaService;
    @Autowired
    private GradeModelElementService gradeModelElementService;
    @Autowired
    private GradeModelConfService gradeModelConfService;
    @Autowired
    private GradeModelInfoService gradeModelInfoService;
    @Autowired
    private GradeModelAO gradeModelAO;

    public GradeJob() {
    }

    @AfsJobHandler(value = "autoGrade")
    public ReturnT<String> autoGrade(String timeDimensionAndTime) {
        log.info("*******autoGrade定时任务参数timeDimensionAndTime=" + timeDimensionAndTime + "********");
        String lastDayOfMonth = ""; //月份的当月最后一天
        //系统当前年月日
        String dataStr8 = DateFormatUtils.format(new Date(), "yyyyMMdd"); //例如：20191231
        String dataStr6 = dataStr8.substring(0, 6); //例如：201909
        String timeDimension = "";
        String time = "";
        if (StringUtils.isNotBlank(timeDimensionAndTime)) {
            String[] array = timeDimensionAndTime.split("=");
            if (array != null) {
                timeDimension = array[0];
                //若传参没有指定月份，默认当前月份
                time = StringUtils.isBlank(array[1]) ? dataStr6 : array[1];
                if (time.length() != 6) {
                    log.error("*******autoGrade（月末）定时任务参数=" + time + ",参数格式错误，需要改为6位yyyyMM格式********");
                    return ReturnT.FAIL;
                }
                lastDayOfMonth = this.getSpecifiedLastDayOfMonth(time);
            }
            //月末评级任务
            if ("m".equals(timeDimension)) {
                log.info("******定时任务【（月末）解析大数据文件】启动******");
                List<GradeBigDataQuota> gradeBigDataQuotas = gradeBigDataQuotaService.list(Wrappers.<GradeBigDataQuota>query().lambda()
                        .eq(GradeBigDataQuota::getTimeDimension, "m").likeRight(GradeBigDataQuota::getDataDate, dataStr6));
                if (CollectionUtils.isNotEmpty(gradeBigDataQuotas)) {
                    log.info("******已存在当月数据，定时任务【（月末）解析大数据文件】开始标记历史数据为删除*******");
                    gradeBigDataQuotaService.update(Wrappers.<GradeBigDataQuota>update().lambda().eq(GradeBigDataQuota::getTimeDimension, "m")
                            .likeRight(GradeBigDataQuota::getDataDate, dataStr6).set(true, GradeBigDataQuota::getDelFlag, "1"));
                    List<String> channelCodeList = new ArrayList<String>();
                    for (GradeBigDataQuota bigDataQuota : gradeBigDataQuotas) {
                        if (!channelCodeList.contains(bigDataQuota.getChannelCode())) {
                            channelCodeList.add(bigDataQuota.getChannelCode());
                        }
                    }
                    for (int i = 0; i < channelCodeList.size(); i++) {
                        gradeModelResultService.update(Wrappers.<GradeModelResult>update().lambda().eq(GradeModelResult::getChannelCode, channelCodeList.get(i))
                                .likeRight(GradeModelResult::getGradeDateStr8, dataStr6).set(GradeModelResult::getDelFlag, Constants.DEL_FLAG_YES));
                    }
                }
            } else if ("q".equals(timeDimension)) {
                log.info("******定时任务【（季度）解析大数据文件】启动******");
                List<GradeBigDataQuota> gradeBigDataQuotas = gradeBigDataQuotaService.list(Wrappers.<GradeBigDataQuota>query().lambda()
                        .eq(GradeBigDataQuota::getTimeDimension, "q").likeRight(GradeBigDataQuota::getDataDate, dataStr6));
                if (CollectionUtils.isNotEmpty(gradeBigDataQuotas)) {
                    log.error("******已存在当前季度数据，定时任务【（季度）解析大数据文件】开始标记历史数据为删除*******");
                    gradeBigDataQuotaService.update(Wrappers.<GradeBigDataQuota>update().lambda().eq(GradeBigDataQuota::getTimeDimension, "q")
                            .likeRight(GradeBigDataQuota::getDataDate, dataStr6).set(true, GradeBigDataQuota::getDelFlag, "1"));
                    List<String> channelCodeList = new ArrayList<String>();
                    for (GradeBigDataQuota bigDataQuota : gradeBigDataQuotas) {
                        if (!channelCodeList.contains(bigDataQuota.getChannelCode())) {
                            channelCodeList.add(bigDataQuota.getChannelCode());
                        }
                    }
                    for (int i = 0; i < channelCodeList.size(); i++) {
                        gradeModelResultService.update(Wrappers.<GradeModelResult>update().lambda().eq(GradeModelResult::getChannelCode, channelCodeList.get(i))
                                .likeRight(GradeModelResult::getGradeDateStr8, dataStr6).set(GradeModelResult::getDelFlag, Constants.DEL_FLAG_YES));
                    }
                }
            } else if ("h".equals(timeDimension)) {
                log.info("******定时任务【（半年）解析大数据文件】启动*******");
                List<GradeBigDataQuota> gradeBigDataQuotas = gradeBigDataQuotaService.list(Wrappers.<GradeBigDataQuota>query().lambda()
                        .eq(GradeBigDataQuota::getTimeDimension, "h")
                        .likeRight(GradeBigDataQuota::getDataDate, dataStr6));
                if (CollectionUtils.isNotEmpty(gradeBigDataQuotas)) {
                    log.error("******已存在当前半年数据，定时任务【（半年）解析大数据文件】开始标记历史数据为删除*******");
                    gradeBigDataQuotaService.update(Wrappers.<GradeBigDataQuota>update().lambda().eq(GradeBigDataQuota::getTimeDimension, "h")
                            .likeRight(GradeBigDataQuota::getDataDate, dataStr6).set(true, GradeBigDataQuota::getDelFlag, "1"));
                    List<String> channelCodeList = new ArrayList<String>();
                    for (GradeBigDataQuota bigDataQuota : gradeBigDataQuotas) {
                        if (!channelCodeList.contains(bigDataQuota.getChannelCode())) {
                            channelCodeList.add(bigDataQuota.getChannelCode());
                        }
                    }
                    for (int i = 0; i < channelCodeList.size(); i++) {
                        gradeModelResultService.update(Wrappers.<GradeModelResult>update().lambda().eq(GradeModelResult::getChannelCode, channelCodeList.get(i))
                                .likeRight(GradeModelResult::getGradeDateStr8, dataStr6).set(GradeModelResult::getDelFlag, Constants.DEL_FLAG_YES));
                    }
                }
            } else if ("y".equals(timeDimension)) {
                log.info("******定时任务【（年度）解析大数据文件】启动*******");
                List<GradeBigDataQuota> gradeBigDataQuotas = gradeBigDataQuotaService.list(Wrappers.<GradeBigDataQuota>query().lambda()
                        .eq(GradeBigDataQuota::getTimeDimension, "y")
                        .likeRight(GradeBigDataQuota::getDataDate, dataStr6));
                if (CollectionUtils.isNotEmpty(gradeBigDataQuotas)) {
                    log.error("******已存在当前年度数据，定时任务【（年度）解析大数据文件】开始标记历史数据为删除*******");
                    gradeBigDataQuotaService.update(Wrappers.<GradeBigDataQuota>update().lambda().eq(GradeBigDataQuota::getTimeDimension, "y")
                            .likeRight(GradeBigDataQuota::getDataDate, dataStr6).set(true, GradeBigDataQuota::getDelFlag, "1"));
                    List<String> channelCodeList = new ArrayList<String>();
                    for (GradeBigDataQuota bigDataQuota : gradeBigDataQuotas) {
                        if (!channelCodeList.contains(bigDataQuota.getChannelCode())) {
                            channelCodeList.add(bigDataQuota.getChannelCode());
                        }
                    }
                    for (int i = 0; i < channelCodeList.size(); i++) {
                        gradeModelResultService.update(Wrappers.<GradeModelResult>update().lambda().eq(GradeModelResult::getChannelCode, channelCodeList.get(i))
                                .likeRight(GradeModelResult::getGradeDateStr8, dataStr6).set(GradeModelResult::getDelFlag, Constants.DEL_FLAG_YES));
                    }
                }
            } else {
                log.error("定时任务时间维度参数配置错误，不能为空");
                return ReturnT.FAIL;
            }
        } else {
            log.error("定时任务时间维度参数配置错误，不能为空");
            return ReturnT.FAIL;
        }
        try {

            //1、sftp下载大数据生成的文件
            SFTPChannel sftpChannel = new SFTPChannel(ip, null, user, pwd, null, null, sftpReadDir, null);
            List<String> filelist = new ArrayList<String>();
            //本地test
//            filelist.add("/Users/jiangxiongfei/data/grade/cis/渠道评级_20200930.txt");
            try {
                String sftpReadDirReal = sftpReadDir + lastDayOfMonth; //例如：/data/dep/cis/20191231
                filelist = sftpChannel.batchDownLoadJpmFile(sftpReadDirReal, downLoadFileSavePath, "渠道评级_" + time);//从长城SFTP下载文件，并获取以:渠道评级_yyyyMM格式开头的所有文件路径
                log.info("文件路径集合filelist个数="+filelist.size());
            } catch (Exception e) {
                log.error("sftp下载大数据渠道评级文件异常" + e.getMessage(), e);
                return ReturnT.FAIL;
//                throw new Exception("sftp下载大数据渠道评级文件异常");
            } finally {
                sftpChannel.disconnect();
            }

            //2、解析文件获取GradeBigDataQuota集合、以及指标参数集合
            List<GradeBigDataQuota> totalList = new ArrayList<GradeBigDataQuota>();
            Map<String, List<GradeDataETVO>> gradeDataETVOListMap = new HashMap<String, List<GradeDataETVO>>();
            Map<String, GradeBigDataQuota> bigDataQuotaMap = new HashMap<String, GradeBigDataQuota>();
            if (CollectionUtils.isNotEmpty(filelist)){
                for (String localFileSavepath : filelist) {
                    //解析大数据返回文件
                    log.info("开始遍历解析大数据文件：【"+localFileSavepath+"】");
                    GradeBigDataQuotaDTO dto = this.parseFile(localFileSavepath, timeDimension);
                    if (dto != null) {
                        totalList.addAll(dto.getGradeBigDataQuotas());
                        gradeDataETVOListMap.putAll(dto.getGradeDataETVOListMap());
                        bigDataQuotaMap.putAll(dto.getBigDataQuotaMap());
                    }
                }
            }

            //3、批量保存到表
            int totalNum = totalList.size();
            if (totalNum > 0 && totalNum <= 1000) {
                gradeBigDataQuotaService.saveBatch(totalList);
            } else if (totalNum > 1000) {
                int totalPages = totalNum / 1000;
                if (totalNum % 1000 != 0) {
                    totalPages++;
                }
                for (int i = 0; i < totalPages; i++) {
                    int fromIndex = i * 1000;
                    int toIndex = fromIndex + 1000;
                    if (i == totalPages - 1) {
                        toIndex = totalNum;
                    }
                    List sublist = totalList.subList(fromIndex, toIndex);
                    gradeBigDataQuotaService.saveBatch(sublist);
                }
            }

            //4、获取评分模型进行计算评级，保存评级结果
            if (CollectionUtils.isNotEmpty(gradeDataETVOListMap)) {
                for (Map.Entry<String, List<GradeDataETVO>> entry : gradeDataETVOListMap.entrySet()) {
                    //区分出一个渠道的当前月份的某业务类型（新车二手车）所有指标，
                    String channelcodeSuffx = entry.getKey();//带后缀“_new”或“_old”的channelCode
                    GradeBigDataQuota quota = bigDataQuotaMap.get(channelcodeSuffx);

                    List<GradeDataETVO> gradeDataETVOList = entry.getValue();
                    if (CollectionUtils.isNotEmpty(gradeDataETVOList)) {
                        //获取渠道对应的评分模型
                        GradeDataETVO gradeDataETVO = gradeDataETVOList.get(0);
                        if (gradeDataETVO == null || StringUtils.isBlank(gradeDataETVO.getBusinessType()) || StringUtils.isBlank(gradeDataETVO.getChannelBelong())) {
                            log.error("数据异常，获取belongBussiness的值为空");
                            throw new Exception("数据异常，获取belongBussiness的值为空");
                        }
                        List<GradeModelConf> gradeModelConfList = gradeModelConfService.list(Wrappers.<GradeModelConf>query().lambda()
                                .eq(GradeModelConf::getChannelBelong, gradeDataETVO.getChannelBelong())
                                .eq(GradeModelConf::getBusinessType, gradeDataETVO.getBusinessType()));
                        if (CollectionUtils.isNotEmpty(gradeModelConfList)) {
                            GradeResultReturnVO gradeResultReturnVO = new GradeResultReturnVO();
                            gradeResultReturnVO.setModelCode(gradeModelConfList.get(0).getModelCode());
                            gradeResultReturnVO.setTerm(dataStr8.substring(4, 6));//月份数
                            gradeResultReturnVO.setTermType("1");//评级报表类型：1-月份，2-季，3-半年，4-年。目前默认:1-月

                            //把当前渠道的当前月份的所有指标和指标值，拼装map
                            Map<String, GradeDataETVO> gradeInfoMap = new HashMap<String, GradeDataETVO>();
                            for (GradeDataETVO vo : gradeDataETVOList) {
                                gradeInfoMap.put(vo.getCode(), vo);
                            }

                            gradeResultReturnVO = gradeModelAO.handleModelRecordResult(0L, 0L, gradeInfoMap,
                                    gradeModelConfList.get(0).getModelCode(), "0", gradeResultReturnVO, null, "1", quota);
                            //根据得分获取信用等级
                            String grade = gradeModelAO.getGradeByScore(gradeResultReturnVO.getScore());

                            //5、更新等级到评分结果表
                            GradeModelResult result = gradeModelResultService.getOne(Wrappers.<GradeModelResult>query().lambda().eq(GradeModelResult::getId, gradeResultReturnVO.getId()));
                            result.setModelValue(grade);
                            result.setRemark("");
                            gradeModelResultService.updateById(result);
                        } else {
                            log.error("未找到匹配的评分模型，查询条件是channelBelong=" + gradeDataETVO.getChannelBelong() + ",businessType=" + gradeDataETVO.getBusinessType());
                            return ReturnT.FAIL;
                        }
                    } else {
                        log.error("数据异常，指标参数为空");
                        return ReturnT.FAIL;
                    }
                }
            }

            if ("m".equals(timeDimension)) {
                log.info("定时任务【（月末）解析大数据文件】执行成功");
            } else if ("q".equals(timeDimension)) {
                log.info("定时任务【（半年）解析大数据文件】执行成功");
            } else if ("h".equals(timeDimension)) {
                log.info("定时任务【（季度）解析大数据文件】执行成功");
            } else if ("y".equals(timeDimension)) {
                log.info("定时任务【（年度）解析大数据文件】执行成功");
            }
            return ReturnT.SUCCESS;
        } catch (Exception e) {
            if ("m".equals(timeDimension)) {
                log.error("定时任务【（月末）解析大数据文件】执行失败", e);
            } else if ("q".equals(timeDimension)) {
                log.error("定时任务【（半年）解析大数据文件】执行成功", e);
            } else if ("h".equals(timeDimension)) {
                log.error("定时任务【（季度）解析大数据文件】执行成功", e);
            } else if ("y".equals(timeDimension)) {
                log.error("定时任务【（年度）解析大数据文件】执行成功", e);
            }
            return ReturnT.FAIL;
        }
    }

    /**
     * @Description: 获取当前月最后一天:
     * @param: [month]
     * @return: java.lang.String
     * @auther: jiangxiongfei
     * @date: 2020-09-08 20:50
     */
    public String getLastDayOfMonth() {
        Calendar calendar = Calendar.getInstance();
        calendar.set(Calendar.DATE, calendar.getActualMaximum(Calendar.DATE));
        DateFormat format = new SimpleDateFormat("yyyyMMdd");
        return format.format(calendar.getTime());
    }

    /**
     * @Description: 获取指定月份的最后一天
     * @param: [yearMonth]
     * @return: java.lang.String
     * @auther: jiangxiongfei
     * @date: 2020-09-08 20:48
     */
    public String getSpecifiedLastDayOfMonth(String yearMonth) {
        int year = Integer.parseInt(yearMonth.substring(0, 4));
        int month = Integer.parseInt(yearMonth.substring(4, 6));
        Calendar cal = Calendar.getInstance();
        // 设置年份
        cal.set(Calendar.YEAR, year);
        // 设置月份
        // cal.set(Calendar.MONTH, month - 1);
        cal.set(Calendar.MONTH, month); //设置当前月的上一个月
        // 获取某月最大天数
        //int lastDay = cal.getActualMaximum(Calendar.DATE);
        int lastDay = cal.getMinimum(Calendar.DATE); //获取月份中的最小值，即第一天
        // 设置日历中月份的最大天数
        //cal.set(Calendar.DAY_OF_MONTH, lastDay);
        cal.set(Calendar.DAY_OF_MONTH, lastDay - 1); //上月的第一天减去1就是当月的最后一天
        // 格式化日期
        SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd");
        return sdf.format(cal.getTime());
    }

    /**
     * @Description: 解析sftp下载后的本地大数据文件
     * @param: [localFileSavepath, timeDimension-时间维度（m-月，h-半年，q-季，y-年）,配置在定时任务配置中]
     * @return: boolean
     * @auther: jiangxiongfei
     * @date: 2020-08-14 17:25
     */
    private GradeBigDataQuotaDTO parseFile(String localFileSavepath, String timeDimension) throws Exception {
        log.info("=================开始解析大数据文件，开始时间：" + System.currentTimeMillis() + "=====================");
        GradeBigDataQuotaDTO dto = new GradeBigDataQuotaDTO();
        List<GradeBigDataQuota> gradeBigDataQuotaList = new ArrayList<GradeBigDataQuota>();
        Map<String, List<GradeDataETVO>> map = new HashMap<String, List<GradeDataETVO>>();
        Map<String, GradeBigDataQuota> bigDataQuotaMap = new HashMap<String, GradeBigDataQuota>();
        BufferedReader reader = null;
        try {
            reader = new BufferedReader(new InputStreamReader(new FileInputStream(localFileSavepath)));  //todo 编码格式
            String record = null;
            int i = 0;
            //1、一次读入一行
            while ((record = reader.readLine()) != null) {
                //第一行是标题行，跳过
                if (i == 0) {
                    i++;
                    continue;
                }
                String[] arr = record.split("\\|\\|");
                //2、将文件转换为GradeBigDataQuota对象集合，目前只处理时间维度为【月】的数据
                //arr[4]:时间维度（m-月，h-半年，q-季，y-年）
                if (StringUtils.isNotBlank(arr[4]) && timeDimension.equals(arr[4])) {
                    GradeBigDataQuota gradeBigDataQuota = new GradeBigDataQuota();
                    //arr[0]-数据日期
                    gradeBigDataQuota.setDataDate(arr[0]);
                    //arr[1]-渠道合作商代码
                    gradeBigDataQuota.setChannelCode(arr[1]);
                    //arr[0]-渠道全称
                    gradeBigDataQuota.setChannelFullName(arr[2]);
                    //arr[3]:指标类型,目前9个,contractQuantityRanking-合同量排名,taskAchievementRate-任务达成率,approvalRate-审批通过率,contractConversionRate-合同转换率,
                    //defectRate-瑕疵率，filingRate-归档率，preLoanFraudRate-贷前欺诈率，overdueRate-30+逾期率，postLoanFraud-贷后欺诈量
                    gradeBigDataQuota.setElementType(arr[3]);
                    //arr[4]-时间维度,m-月，h-半年，q-季，y-年
                    gradeBigDataQuota.setTimeDimension(arr[4]);
                    //arr[5]-渠道归属（00-sp，01-直营车商，02-总对总）")
                    gradeBigDataQuota.setChannelBelong(arr[5]);
                    //arr[6]-业务类型（01-新车，02-二手车）
                    gradeBigDataQuota.setBusinessType(arr[6]);
                    //arr[7]-指标值
                    gradeBigDataQuota.setValue(StringUtils.isBlank(arr[7]) ? BigDecimal.ZERO : new BigDecimal(arr[7]));
                    gradeBigDataQuota.setCreateBy("channel job");
                    gradeBigDataQuota.setCreateTime(new Date());

                    gradeBigDataQuotaList.add(gradeBigDataQuota);

                    //todo 3、退网合作商的不能评级，查询渠道状态校验。目前退网功能二期开发，暂不处理，

                    if (StringUtils.isNotBlank(arr[1]) && StringUtils.isNotBlank(arr[5]) && StringUtils.isNotBlank(arr[6])) {
                        //页面能传模型代码ModelCode过来，但是定时任务没有，需要通过大数据获取评估模型
                        List<GradeModelConf> gradeModelConfList = gradeModelConfService.list(Wrappers.<GradeModelConf>query().lambda()
                                .eq(GradeModelConf::getChannelBelong, arr[5])
                                .eq(GradeModelConf::getBusinessType, arr[6])
                                .eq(GradeModelConf::getTimeDimension, arr[4]));
                        if (CollectionUtils.isEmpty(gradeModelConfList)) {
                            log.error("解析数据时未找到匹配的评估模型,查询条件-->gradeBigDataQuota.businessType=" + gradeBigDataQuota.getBusinessType()
                                    + ",channelBelong=" + gradeBigDataQuota.getChannelBelong() + ",timeDimension=" + gradeBigDataQuota.getTimeDimension());
                            throw new Exception("解析数据时未找到匹配的评估模型");
                        } else if (gradeModelConfList.size() == 1) {
                            //查询模型状态
                            GradeModelInfo modelInfo = gradeModelInfoService.getOne(Wrappers.<GradeModelInfo>query().lambda()
                                    .eq(GradeModelInfo::getModelCode, gradeModelConfList.get(0).getModelCode())
                                    .eq(GradeModelInfo::getTimeDimension, gradeModelConfList.get(0).getTimeDimension()));
                            if (!Constants.VALID_FLAG_VALID_1.equals(modelInfo.getState())) {
                                log.error("模型代码【" + gradeModelConfList.get(0).getModelCode() + "】不是有效状态");
                                throw new Exception("模型代码【" + gradeModelConfList.get(0).getModelCode() + "】不是有效状态");
                            }
                        } else {
                            log.error("数据异常，解析数据时找到多个匹配的评估模型,查询条件-->gradeBigDataQuota.businessType=" + gradeBigDataQuota.getBusinessType()
                                    + ",channelBelong=" + gradeBigDataQuota.getChannelBelong() + ",timeDimension=" + gradeBigDataQuota.getTimeDimension());
                            throw new Exception("解析数据时未找到匹配的评估模型");
                        }

                        //通过指标类型获取elementNo
                        GradeModelElement gradeModelElement = gradeModelElementService.getOne(Wrappers.<GradeModelElement>query().lambda()
                                .eq(GradeModelElement::getModelCode, gradeModelConfList.get(0).getModelCode())
                                .eq(GradeModelElement::getElementCode, gradeBigDataQuota.getElementType()));
                        if (gradeModelElement == null) {
                            log.error("未找到评分指标GradeModelElement,查询条件：modeCode=" + gradeModelConfList.get(0).getModelCode() + ",ElementCode=" + gradeBigDataQuota.getElementType());
//                            throw new Exception("未找到评分指标GradeModelElement,查询条件：modeCode=" + gradeModelConfList.get(0).getModelCode() + ",ElementCode=" + gradeBigDataQuota.getElementType());
                        }else{
                            //缓存指标到map中，key-渠道code+业务类型（新车二手车），value-指标GradeDataETVO集合
                            GradeDataETVO gradeDataETBean = new GradeDataETVO();
                            gradeDataETBean.setKey(gradeModelElement.getElementNo());
                            gradeDataETBean.setCode(gradeModelElement.getElementNo());//通过指标类型获取elementNo
                            gradeDataETBean.setChannelBelong(arr[5]);
                            gradeDataETBean.setBusinessType(arr[6]);
                            gradeDataETBean.setValue(arr[7]);
                            //新车、二手车判断
                            if ("01".equals(arr[6])) { //新车
                                //获取某个渠道类型（归属）+业务类型下所有的指标，并map，供后续评分用
                                List<GradeDataETVO> gradeDataETVOList = map.get(arr[1] + "_new");
                                if (gradeDataETVOList != null) {
                                    gradeDataETVOList.add(gradeDataETBean);
                                } else {
                                    gradeDataETVOList = new ArrayList<GradeDataETVO>();
                                    gradeDataETVOList.add(gradeDataETBean);
                                    map.put(arr[1] + "_new", gradeDataETVOList);
                                }

                                //一个渠道一个业务类型只需要map一条指标记录就可以，后面评分计算只用到channelcode和belongBussiness,任取一个指标就能获取到这2个值
                                GradeBigDataQuota bigDataQuota = bigDataQuotaMap.get(arr[1] + "_new");
                                if (bigDataQuota == null) {
                                    bigDataQuotaMap.put(arr[1] + "_new", gradeBigDataQuota);
                                }
                            } else if ("02".equals(arr[6])) { //二手车
                                //获取某个channelCode+业务类型下所有的指标，并map，供后续评分用
                                List<GradeDataETVO> gradeDataETVOList = map.get(arr[1] + "_old");
                                if (gradeDataETVOList != null) {
                                    gradeDataETVOList.add(gradeDataETBean);
                                } else {
                                    gradeDataETVOList = new ArrayList<GradeDataETVO>();
                                    gradeDataETVOList.add(gradeDataETBean);
                                    map.put(arr[1] + "_old", gradeDataETVOList);
                                }

                                //一个渠道一个业务类型只需要map一条指标记录就可以，后面评分计算只用到channelcode和belongBussiness,任取一个指标就能获取到这2个值
                                GradeBigDataQuota bigDataQuota = bigDataQuotaMap.get(arr[1] + "_old");
                                if (bigDataQuota == null) {
                                    bigDataQuotaMap.put(arr[1] + "_old", gradeBigDataQuota);
                                }
                            }
                        }
                    } else {
                        log.error("提供的大数据异常，渠道编码或者渠道归属数据为空");
                        reader.close();
                        throw new Exception("提供的大数据异常，渠道编码或者渠道归属数据为空");
                    }
                }
            }
            reader.close();
            //3、提取map中的GradeBigDataQuota对象，转换为集合
            dto.setGradeBigDataQuotas(gradeBigDataQuotaList);
            dto.setGradeDataETVOListMap(map);
            dto.setBigDataQuotaMap(bigDataQuotaMap);
            log.info("=================解析大数据文件结束时间：" + System.currentTimeMillis() + "=====================");
            return dto;
        } catch (Exception e) {
            log.info("=================解析异常，解析大数据文件结束时间：" + System.currentTimeMillis() + "=====================");
            log.error("解析大数据文件失败" + e);
            throw new Exception("解析大数据文件失败");
        } finally {
            if (reader != null) {
                try {
                    reader.close();
                } catch (IOException e1) {
                    log.error(e1.getMessage(), e1);
                }
            }
        }
    }

}